/*
Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package fschmidt.util.mail;


// because javamail sucks

public final class MailHome {
	private MailHome() {}  // never

	private static MailFactory mailFactory = new fschmidt.util.mail.javamail.MailFactoryImpl();
	private static SmtpServer defaultSmtpServer = null;

	public static void setMailFactory(MailFactory mailFactory) {
		MailHome.mailFactory = mailFactory;
	}

	public static SmtpServer getSmtpServer(String machineName) {
		return mailFactory.getSmtpServer(machineName);
	}

	public static SmtpServer getSmtpServer(String machineName,String username,String password) {
		return mailFactory.getSmtpServer(machineName,username,password);
	}
	
	public static SmtpServer getSmtpServerPool(String[] machineNames) {
		SmtpServer[] servers = new SmtpServer[machineNames.length];
		for (int i=0; i<servers.length; i++) {
			servers[i] = mailFactory.getSmtpServer(machineNames[i]);
		}
		return new SmtpServerPool(servers);
	}
	
	public static SmtpServer getSmtpServerPool(String[] machineNames,String username,String password) {
		SmtpServer[] servers = new SmtpServer[machineNames.length];
		for (int i=0; i<servers.length; i++) {
			servers[i] = mailFactory.getSmtpServer(machineNames[i],username,password);
		}
		return new SmtpServerPool(servers);
	}
	
	public static Pop3Server getPop3Server(String machineName,String username,String password) {
		return mailFactory.getPop3Server(machineName,username,password);
	}

	public static Pop3Server getPop3ServerPool(String[] machineNames,String username,String password) {
		Pop3Server[] servers = new Pop3Server[machineNames.length];
		for (int i=0; i<servers.length; i++) {
			servers[i] = mailFactory.getPop3Server(machineNames[i],username,password);
		}
		return new Pop3ServerPool(servers);
	}
	
	public synchronized static void setDefaultSmtpServer(SmtpServer smtpServer) {
		MailHome.defaultSmtpServer = smtpServer;
	}

	public synchronized static SmtpServer getDefaultSmtpServer() {
		if( defaultSmtpServer==null ) {
			String server = System.getProperty("mail.smtp.host");
			if( server==null )
				return null;
			defaultSmtpServer = getSmtpServer(server);
		}
		return defaultSmtpServer;
	}

	public static Mail newMail() {
		return mailFactory.newMail();
	}

	public static Mail newMail(String rawInput) {
		return mailFactory.newMail(rawInput);
	}

	public static MailAddress parseAddress(String addr) throws MailException {
		return mailFactory.parseAddress(addr);
	}

	public static boolean isSendingMail() {
		return mailFactory.isSendingMail();
	}
}
